<template>
  <div>
    <x-card v-if="hasPerm('children:page')">
      <div slot="content" class="table-page-search-wrapper">
        <a-form layout="inline">
          <a-row :gutter="20">
            <a-col :md="6" :sm="24">
              <a-input v-model="queryParam.ChildName" allow-clear placeholder="请输入学生名称" />
            </a-col>
            <a-col :md="6" :sm="24">
              <a-select allowClear v-model="queryParam.ClassId" placeholder="请选择班级">
                <a-select-option v-for="item in classList" :key="item.id" :value="item.id">{{
                  item.name
                }}</a-select-option>
              </a-select>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-select allowClear v-model="queryParam.State" placeholder="请选择状态">
                <a-select-option :value="1">未入班幼儿</a-select-option>
                <a-select-option :value="2">在读幼儿</a-select-option>
                <a-select-option :value="3">毕业幼儿</a-select-option>
              </a-select>
            </a-col>
            <a-col :md="2" :sm="24">
              <a-button type="primary" @click="$refs.table.refresh(true)">查询</a-button>
            </a-col>
          </a-row>
        </a-form>
      </div>
    </x-card>
    <a-card :bordered="false">
      <s-table
        ref="table"
        :columns="columns"
        :data="loadData"
        :alert="options.alert"
        :rowKey="record => record.id"
        :rowSelection="options.rowSelection"
      >
        <template slot="operator" v-if="hasPerm('children:add')">
          <a-button @click="$refs.addForm.add()" icon="plus" type="primary" v-if="hasPerm('children:add')"
            >新增幼儿</a-button
          >
          <a-button
            type="danger"
            :disabled="selectedRowKeys.length < 1"
            v-if="hasPerm('sysclasses:delete')"
            @click="batchDelete"
            ><a-icon type="delete" />批量删除</a-button
          >
          <x-down style="display:none" v-if="hasPerm('children:export')" ref="batchExport" @batchExport="batchExport" />
        </template>
        <span slot="action" slot-scope="text, record">
          <a v-if="hasPerm('children:check')" @click="$refs.editForm.edit(record)">查看</a>
          <a-divider type="vertical" v-if="hasPerm('children:edit') & hasPerm('children:check')" />
          <a v-if="hasPerm('children:edit')" @click="$refs.editForm.edit(record)">编辑</a>
          <a-divider type="vertical" v-if="hasPerm('children:shifttransfers') & hasPerm('children:edit')" />
          <a v-if="hasPerm('children:shifttransfers')" @click="$refs.zhuanbanForm.show(record, classList)">转班</a>
          <a-divider type="vertical" v-if="hasPerm('children:shifttransfers') & hasPerm('children:leaveclass')" />
          <a-popconfirm
            v-if="hasPerm('children:leaveclass')"
            placement="topRight"
            title="确定要离班吗？"
            @confirm="() => liban(record)"
          >
            <a v-if="hasPerm('children:leaveclass')">离班</a>
          </a-popconfirm>
        </span>
      </s-table>
      <add-form ref="addForm" :classList="classList" @ok="handleOk" />
      <edit-form ref="editForm" :classList="classList" @ok="handleOk" />
      <zhuanban ref="zhuanbanForm" @ok="handleOk"></zhuanban>
    </a-card>
  </div>
</template>

<script>
import { STable, XCard, XDown } from '@/components'
import { sysPosList } from '@/api/modular/garden/classManage'
import { childrenPage, childrenDelete, childrenLeaveClass } from '@/api/modular/children/childManage'
import addForm from './addForm'
import editForm from './editForm'
import zhuanban from './zhuanban'

export default {
  components: {
    XDown,
    XCard,
    STable,
    addForm,
    editForm,
    zhuanban
  },

  data() {
    return {
      // 查询参数
      queryParam: {
        State: 2
      },
      // 表头
      columns: [
        {
          title: '幼儿姓名',
          dataIndex: 'name'
        },
        {
          title: '学号',
          dataIndex: 'studentNumber'
        },
        {
          title: '出生日期',
          dataIndex: 'birthdayStr'
        },
        {
          title: '所在班级',
          dataIndex: 'className'
        },
        {
          title: '登记日期',
          dataIndex: 'registeredDay'
        }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        return childrenPage(Object.assign(parameter, this.queryParam)).then(res => {
          return res.data
        })
      },
      selectedRowKeys: [],
      selectedRows: [],
      options: {
        alert: {
          show: true,
          clear: () => {
            this.selectedRowKeys = []
          }
        },
        rowSelection: {
          selectedRowKeys: this.selectedRowKeys,
          onChange: this.onSelectChange
        }
      },
      classList: []
    }
  },

  created() {
    if (this.hasPerm('children:edit')) {
      this.columns.push({
        title: '操作',
        width: '200px',
        dataIndex: 'action',
        scopedSlots: { customRender: 'action' }
      })
    }
    this.getClass()
  },
  methods: {
    getClass() {
      let obj = {
        isArchived: false
      }
      sysPosList(obj).then(res => {
        this.classList = res.data
      })
    },

    /**
     * 单个删除
     */
    singleDelete(record) {
      const param = [{ id: record.id }]
      this.sysPosDelete(param)
    },
    /**
     * 批量删除
     */
    batchDelete() {
      const paramIds = this.selectedRowKeys.map(d => {
        return { id: d }
      })
      this.sysPosDelete(paramIds)
    },
    /**
     * 删除
     */
    sysPosDelete(param) {
      childrenDelete(param)
        .then(res => {
          if (res.success) {
            this.$message.success('删除成功')
            this.$refs.table.clearRefreshSelected()
          } else {
            this.$message.error('删除失败：' + res.message)
          }
        })
        .catch(err => {
          this.$message.error('删除错误：' + err.message)
        })
    },
    showHuamingce() {},
    /**
     * 批量导出
     */
    batchExport() {
      const paramIds = this.selectedRowKeys.map(d => {
        return { id: d }
      })
      sysPosExport(paramIds).then(res => {
        this.$refs.batchExport.downloadfile(res)
      })
    },
    handleOk() {
      this.$refs.table.refresh()
    },
    onSelectChange(selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
    },
    liban(record) {
      let postData = {
        id: record.id
      }
      childrenLeaveClass(postData).then(res => {
        if (res.code === 200) {
          this.$refs.table.clearRefreshSelected()
          this.$message.success('离班成功')
        } else {
          this.$message.error('离班失败：' + res.message)
        }
      })
    }
  }
}
</script>

<style lang="less">
.table-operator {
  margin-bottom: 18px;
}
button {
  margin-right: 8px;
}
</style>
